﻿using NDK.Motion.MachineManager;
using NDK.Motion.MachineResources;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace NDK.Motion.Frm
{
    public partial class Frm_ModifyStation : Form
    {
        private static log4net.ILog traceLogger = log4net.LogManager.GetLogger("Frm_ModifyStation");

        public Station station = new Station();
        public Frm_ModifyStation()
        {
            InitializeComponent();
        }

        private void Frm_ModifyStation_Load(object sender, EventArgs e)
        {
            foreach (var axis in MachineDataManager.Instance.AxisList)
            {
                cb_Axis.Items.Add(axis.Name);
            }
            tb_StationName.Text = station.StationName;
            tb_Axis.Text = station.Axes;
        }

        private void btn_Confirm_BtnClick(object sender, EventArgs e)
        {
            string stationName = tb_StationName.Text.Trim();
            //从属轴不能为空
            if (string.IsNullOrEmpty(tb_Axis.Text.Trim()))
            {
                lbl_AxisTip.ForeColor = Color.Red;
                tb_Axis.Focus();
                return;
            }
            //工位名称不能为空
            lbl_AxisTip.ForeColor = Color.Black;
            if (string.IsNullOrWhiteSpace(stationName))
            {
                lbl_StationTip.ForeColor = Color.Red;
                tb_StationName.Focus();
                return;
            }
            lbl_StationTip.ForeColor = Color.Black;
            //查找要修改的轴
            Station station = MachineDataManager.Instance.StationList.SingleOrDefault(s => s.StationName == stationName);
            if (station == null) return;
            //修改名称（目前禁止修改）
            station.StationName = tb_StationName.Text.Trim();
            station.AxisList.Clear();
            //修改轴
            string axes = tb_Axis.Text;
            if (axes.Contains(","))
            {
                List<string> axisNameList = axes.Split(',').ToList();
                foreach (var axisName in axisNameList)
                {
                    Axis axis = MachineDataManager.Instance.AxisList.SingleOrDefault(a => a.Name == axisName);
                    station.AxisList.Add(axis);
                }
            }
            else
            {
                Axis axis = MachineDataManager.Instance.AxisList.SingleOrDefault(a => a.Name == axes);
                if (axis != null)
                {
                    station.AxisList.Add(axis);
                }
            }
            //MachineDataManager.Instance.StationList.Add(station);

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void cb_Axis_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!tb_Axis.Text.Contains(cb_Axis.Text))
            {
                if (string.IsNullOrEmpty(tb_Axis.Text))
                {
                    tb_Axis.Text = (cb_Axis.Text);
                }
                else
                {
                    tb_Axis.Text += ("," + cb_Axis.Text);
                }
            }
        }
    }
}
